/**
 * Created by think on 2017/1/1.
 */
/**
 * Created by Administrator on 2016/12/25.
 */

//范围随机数
exports.randomRange = function(start,end){
    return Math.floor(Math.random()*(end-start+1))+start;
};

//随机数
exports.randomNum = function (num){
    return Math.floor(Math.random()*(num+1));
};

//几年
exports.getTimeFormat = function(startTime) {
    var startTimeMills = this.date(startTime).getTime();
    var endTimeMills = new Date().getTime();
    var diff = parseInt((endTimeMills - startTimeMills) / 1000);//秒
    var day_diff = parseInt(Math.floor(diff / 86400));//天
    var buffer = Array();
    if (day_diff < 0) {
        return "[error],时间越界...";
    } else {
        if (day_diff == 0 && diff < 60) {
            if (diff <= 0)
                diff = 1;
            buffer.push(diff + "秒前");
        } else if (day_diff == 0 && diff < 120) {
            buffer.push("1 分钟前");
        } else if (day_diff == 0 && diff < 3600) {
            buffer.push(Math.round(Math.floor(diff / 60)) + "分钟前");
        } else if (day_diff == 0 && diff < 7200) {
            buffer.push("1小时前");
        } else if (day_diff == 0 && diff < 86400) {
            buffer.push(Math.round(Math.floor(diff / 3600)) + "小时前");
        } else if (day_diff == 1) {
            buffer.push("1天前");
        } else if (day_diff < 7) {
            buffer.push(day_diff + "天前");
        } else if (day_diff < 30) {
            buffer.push(Math.round(Math.floor(day_diff / 7)) + " 星期前");
        } else if (day_diff >= 30 && day_diff <= 179) {
            buffer.push(Math.round(Math.floor(day_diff / 30)) + "月前");
        } else if (day_diff >= 180 && day_diff < 365) {
            buffer.push("半年前");
        } else if (day_diff >= 365) {
            buffer.push(Math.round(Math.floor(day_diff / 30 / 12)) + "年前");
        }
    }
    return buffer.toString();
};

/*转换日期格式*/
exports.transferDate = function(date){
    if(typeof date =="string"){
        return new Date(date.replace(/-/ig,"/").replace("T"," "));
    }else{
        return date;
    }
};


module.exports = {
    rand1:(a,b)=>this.randomRange(a,b),
    rand2:this.randomNum,
    time:this.getTimeFormat,
    date:this.transferDate
};